﻿using Core.Rendering.Resources;
using OpenTK.Graphics.OpenGL4;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Core.Rendering.Buffer
{
    public class UniformBufferObject
    {
        private int _id;

        public UniformBufferObject(int size = 1024,int bindingPoint = 0)
        {
            _id = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.UniformBuffer, _id);
            GL.BufferData(BufferTarget.UniformBuffer, size, 0, BufferUsageHint.DynamicDraw);
            GL.BindBuffer(BufferTarget.UniformBuffer, 0);
            GL.BindBufferBase(BufferRangeTarget.UniformBuffer, bindingPoint, _id);
        }
        public void Bind()
        {
            GL.BindBuffer(BufferTarget.UniformBuffer, _id);
        }
        public static void Unbind()
        {
            GL.BindBuffer(BufferTarget.UniformBuffer, 0);
        }
        public static void BindingPointToShader(Shader shader,string name,int bindingPoint)
        {
            int index = GL.GetUniformBlockIndex(shader.Id, name);
            GL.UniformBlockBinding(shader.Id, index,bindingPoint);
        }
        public void SetData<T>(T data,int offset)
            where T : struct
        {
            Bind();
            GL.BufferSubData(BufferTarget.UniformBuffer, offset, Marshal.SizeOf<T>(), ref data);
            Unbind();
        }
    }
}
